//
//  TLTransformSceneViewController.swift
//  TLiOS26LiquidGlassDemo
//
//  Created by jiayin on 2025/8/27.
//

import UIKit
import SnapKit

/**
 * 一、介绍
 * WWDC24 中 UIViewController 增加了 5 种特殊的转场效果zoom、coverVertical、flipHorizontal、crossDissolve与partialCurl。但是触发条件仅限于 UIView，WWDC25 将zoom的触发条件扩展至 UIBarButtonItem。
 */
class TLTransformSceneViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        let barButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(barButtonItemClicked))
        navigationItem.rightBarButtonItem = barButtonItem
    }

    @objc func barButtonItemClicked(_ sender: Any) {
        let nextViewController = NextViewController()
//        nextViewController.view.backgroundColor = .systemRed
        nextViewController.preferredTransition = .zoom { context in
            guard context.zoomedViewController is NextViewController else {
                fatalError("Unable to access the current UIViewController.")
            }
            // iOS26新增，返回触发的UIBarButtonItem
            return self.navigationItem.rightBarButtonItem
        }
        present(nextViewController, animated: true)
    }
}

class NextViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .systemGreen
        
        // 帮我添加一个label，显示信息，单击我页面会消失
        // 1. 创建 UILabel
        let label = UILabel()
        label.text = "单击我页面会消失"
        label.textAlignment = .center
        label.isUserInteractionEnabled = true // 允许交互（以便点击）
        view.addSubview(label)
        
        // 2. 使用 SnapKit 布局
        label.snp.makeConstraints { make in
            make.center.equalToSuperview() // 居中
            make.width.equalTo(200)       // 宽度 200
            make.height.equalTo(50)       // 高度 50
        }
        
        // 3. 添加点击手势
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleLabelTap))
        label.addGestureRecognizer(tapGesture)
        
    }
    
    // 4. 点击事件处理
    @objc private func handleLabelTap() {
        dismiss(animated: true) // 点击后关闭当前页面
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        dismiss(animated: true)
    }
}
